package org.mnswf.ui.preloader {
	
	import com.dannypatterson.config.Settings;
	import com.dannypatterson.remoting.FaultEvent;
	import com.dannypatterson.remoting.ResultEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;
	
	import org.mnswf.model.Member;
	import org.mnswf.services.GetMemberService;

	public class Preloader extends DownloadProgressBar {
		
		private var getMemberService:GetMemberService;
		
		static public var member:Member;
		
		public function Preloader() {}
        
		override public function set preloader(preloader:Sprite):void {         	      
			//preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress, false, 0, true);    
			//preloader.addEventListener(Event.COMPLETE, onSWFDownloadComplete, false, 0, true);
			//preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress, false, 0, true);
			preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete, false, 0, true);
		}
    	
		//private function onSWFDownloadProgress(event:ProgressEvent):void {
 		//	var percent:Number = Math.round((event.bytesLoaded/event.bytesTotal) * 100);
		//}
    	
		//private function onSWFDownloadComplete(event:Event):void {
		//	Log.getLogger("Preloader").info("Download completed");
		//}
    	
		//private function onFlexInitProgress(event:Event):void {
		//	Log.getLogger("Preloader").info("Beginning application initialization");
		//}

		private function onFlexInitComplete(event:Event):void {
			Settings.getInstance().addEventListener(Event.INIT, onSettingsInitComplete, false, 0, true);
			Settings.getInstance().loadSettings("xml/config.xml");
		}
		
		private function onSettingsInitComplete(event:Event):void {
			getMemberService = new GetMemberService();
			getMemberService.addEventListener(ResultEvent.RESULT, onMemberResult, false, 0, true);
			getMemberService.addEventListener(FaultEvent.FAULT, onMemberFault, false, 0, true);
			getMemberService.execute();
		}
		
		private function onMemberResult(event:ResultEvent):void {
			member = Member(event.result);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function onMemberFault(event:FaultEvent):void {
			dispatchEvent(new Event(Event.COMPLETE));
		}

	}
}